/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.application.Platform
 *  javafx.event.ActionEvent
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.AnchorPane
 *  javafx.stage.Stage
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.util.ObjectUtil;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.salesReceipts.PdAndExpController;
import com.gys.controller.salesReceipts.ProductQueryController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.salesReceipts.GetPointExchangeOutData;
import com.gys.data.salesReceipts.GetQueryMemberOutData;
import com.gys.data.salesReceipts.GetQueryProductOutData;
import com.gys.view.salesReceipts.PointExchangeView;
import de.felixroske.jfxsupport.FXMLController;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class PointExchangeController
extends BaseController
implements Initializable {
    @FXML
    private AnchorPane pane;
    @FXML
    private TextField needExNum;
    @FXML
    private TextField proCode;
    @FXML
    private TextField proNam;
    @FXML
    private TextField changeAmt;
    @FXML
    private TextField name;
    @FXML
    private TextField maxExNum;
    @FXML
    private TextField changePoint;
    @FXML
    private TextField residuePoint;
    @FXML
    private TextField point;
    @Autowired
    private PdAndExpController pdAndExpController;
    @Autowired
    private ProductQueryController productQueryController;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    private Timer timer = new Timer();

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.name)) {
            return;
        }
        this.needExNum.clear();
        this.needExNum.textProperty().addListener((observable, oldValue, newValue) -> {
            if (ObjectUtil.isEmpty(newValue)) {
                return;
            }
            if (!CommonUtil.positiveInteger(newValue)) {
                Platform.runLater((Runnable)new Runnable(){

                    @Override
                    public void run() {
                        Alert alert = PointExchangeController.this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u6b63\u6574\u6570");
                        alert.show();
                        PointExchangeController.this.needExNum.clear();
                        PointExchangeController.this.residuePoint.clear();
                    }
                });
                return;
            }
            if (Integer.valueOf(this.needExNum.getText()) > Integer.valueOf(this.maxExNum.getText())) {
                Platform.runLater((Runnable)new Runnable(){

                    @Override
                    public void run() {
                        Alert alert = PointExchangeController.this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u5151\u6362\u6570\u91cf\u8d85\u8fc7\u5f53\u524d\u53ef\u5151\u6362\u6700\u5927\u6570\u91cf");
                        alert.show();
                        PointExchangeController.this.needExNum.clear();
                        PointExchangeController.this.residuePoint.clear();
                    }
                });
                return;
            }
            int pointInt = Integer.valueOf(this.point.getText());
            int needExNumInt = Integer.valueOf(this.needExNum.getText());
            int changePointInt = Integer.valueOf(this.changePoint.getText());
            this.residuePoint.setText(String.valueOf(pointInt - needExNumInt * changePointInt));
        });
        GetQueryMemberOutData memberOutData = (GetQueryMemberOutData)CacheUtil.get("memberOutData");
        if (ObjectUtil.isEmpty(memberOutData)) {
            this.timer.schedule(new TimerTask(){

                @Override
                public void run() {
                    PointExchangeView view = SpringUtil.getBean(PointExchangeView.class);
                    Stage stage = (Stage)view.getView().getScene().getWindow();
                    stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                        public void handle(KeyEvent e) {
                            if (e.getCode() == KeyCode.H) {
                                PointExchangeController.this.salesReceiptsMainController.inputMember();
                            }
                            e.consume();
                        }
                    });
                    Platform.runLater((Runnable)new Runnable(){

                        @Override
                        public void run() {
                            Alert alert = PointExchangeController.this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8be5\u5546\u54c1\u53c2\u52a0\u79ef\u5206\u5151\u6362\u6d3b\u52a8\uff0c\u8bf7\u8f93\u5165\u4f1a\u5458,\u5feb\u6377\u952e<H>");
                            alert.showAndWait();
                        }
                    });
                }
            }, 500L);
        } else {
            this.name.setText(memberOutData.getName());
            this.point.setText(memberOutData.getPoint());
            GetPointExchangeOutData pointExchangeOutData = this.pdAndExpController.getPointExchangeOutData();
            this.changePoint.setText(pointExchangeOutData.getGsieslExchangeIntegra());
            this.changeAmt.setText(pointExchangeOutData.getGsiesExchangeAmt());
            GetQueryProductOutData queryProductOutData = this.salesReceiptsMainController.getProduct();
            this.proNam.setText(queryProductOutData.getProName());
            this.proCode.setText(queryProductOutData.getProCode());
            this.maxExNum.setText("" + Integer.valueOf(this.point.getText()) / Integer.valueOf(this.changePoint.getText()));
            this.needExNum.requestFocus();
        }
    }

    @FXML
    private void sureAction(ActionEvent event) {
        GetQueryMemberOutData memberOutData = (GetQueryMemberOutData)CacheUtil.get("memberOutData");
        if (ObjectUtil.isEmpty(memberOutData)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8be5\u5546\u54c1\u53c2\u52a0\u79ef\u5206\u5151\u6362\u6d3b\u52a8\uff0c\u8bf7\u8f93\u5165\u4f1a\u5458,\u5feb\u6377\u952e<H>");
            alert.showAndWait();
            return;
        }
        if (ObjectUtil.isEmpty(this.needExNum.getText())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u9700\u8981\u5151\u6362\u6570\u91cf");
            alert.showAndWait();
            return;
        }
        int needExNumInt = Integer.valueOf(this.needExNum.getText());
        int changeAmtInt = Integer.valueOf(this.changeAmt.getText());
        this.salesReceiptsMainController.initTableExc(String.valueOf(needExNumInt * changeAmtInt));
        this.cancelAction();
    }

    @FXML
    private void cancelAction() {
        this.pane.getScene().getWindow().hide();
    }
}

